package com.slh.touchandroid.adapter;

import java.util.ArrayList;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageButton;

import com.slh.touchandroid.R;
import com.slh.touchandroid.model.AppInfo;
import com.slh.touchandroid.utils.AppUtils;

public class FavoriteAdapter extends BaseAdapter {
	private Context context;
	private ArrayList<AppInfo> infos;

	public FavoriteAdapter(Context context, ArrayList<AppInfo> infos) {
		super();
		this.context = context;
		this.infos = infos;
	}

	@Override
	public int getCount() {
		return infos.size();
	}

	@Override
	public Object getItem(int position) {
		return infos.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		Holder holder;
		if (convertView == null) {
			convertView = LayoutInflater.from(context).inflate(
					R.layout.cell_favorite_app, null);
			holder = new Holder();
			holder.imgbtnIcon = (ImageButton) convertView
					.findViewWithTag(context.getString(R.string.tag_grid));
			convertView.setTag(holder);
		} else {
			holder = (Holder) convertView.getTag();
		}
		AppInfo info = infos.get(position);
		String action = info.action;
		if (AppInfo.ACTION_BACK.equals(action)) {
			holder.imgbtnIcon.setImageResource(R.drawable.btn_back);
		} else if (AppInfo.ACTION_LAUNCH_FAV_APP.equals(action)) {
			holder.imgbtnIcon.setImageDrawable(AppUtils.getApplicationIcon(
					context, info.pgk));
		} else {
			holder.imgbtnIcon.setImageResource(R.drawable.btn_add);
		}
		return convertView;
	}

	static class Holder {
		ImageButton imgbtnIcon;
	}

}
